﻿using Microsoft.EntityFrameworkCore;

namespace HelenServer.UserCenter.Dal;

public partial class UserCenterDbContext : DbContext
{
    public UserCenterDbContext(DbContextOptions<UserCenterDbContext> options)
        : base(options)
    {

    }

    public virtual DbSet<Role> Roles { get; set; } = null!;
    public virtual DbSet<Department> Departments { get; set; } = null!;
    public virtual DbSet<Position> Positions { get; set; } = null!;
    public virtual DbSet<User> Users { get; set; } = null!;

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<User>(entity =>
        {
            entity.Property(m => m.Department).HasConversion<EntityValueConverter<UserCenterDbContext, Department>, EntityValueComparer<Department>>();
            entity.Property(m => m.Position).HasConversion<EntityValueConverter<UserCenterDbContext, Position>, EntityValueComparer<Position>>();
            entity.Property(m => m.Roles).HasConversion<EntityCollectionValueConverter<UserCenterDbContext, Role>, EntityCollectionValueComparer<Role>>();
        });

        modelBuilder.Entity<Department>(entity =>
        {
            entity.Property(m => m.Parent).HasConversion<EntityValueConverter<UserCenterDbContext, Department>, EntityValueComparer<Department>>();
            entity.Property(m => m.Manager).HasConversion<EntityValueConverter<UserCenterDbContext, User>, EntityValueComparer<User>>();

            entity.Property(m => m.Role).HasConversion<EntityValueConverter<UserCenterDbContext, Role>, EntityValueComparer<Role>>();
        });

        modelBuilder.Entity<Position>(entity =>
        {
            entity.Property(m => m.Role).HasConversion<EntityValueConverter<UserCenterDbContext, Role>, EntityValueComparer<Role>>();
        });

        modelBuilder.Entity<Role>(entity =>
        {
            entity.Property(m => m.Types).HasConversion<CompositStringToStringValueConverter, CompositStringToStringValueComparer>();
        });

        this.OnModelCreatingPartial(modelBuilder);
    }

    partial void OnModelCreatingPartial(ModelBuilder modelBuilder);
}